package com.lc.cehui.ui.activity.land;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.request.ForgetNewpwdApi;
import com.lc.cehui.http.request.GetCodeApi;
import com.lc.cehui.http.response.CodeBean;
import com.lc.cehui.manager.InputTextManager;
import com.lc.cehui.util.CourseUtils;
import com.lc.cehui.widget.CountdownView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ForgetPasswordActivity extends AppActivity {

    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_yzm)
    EditText etYzm;
    @BindView(R.id.cdv_yzm)
    CountdownView cdvYzm;
    @BindView(R.id.bt_submit)
    Button btSubmit;
    @BindView(R.id.bt_cancel)
    Button btCancel;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forget_password;
    }

    @Override
    protected void initView() {
        InputTextManager.with(this)
                .addView(etYzm)
                .addView(etPhone)
                .setMain(btSubmit)
                .build();
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.cdv_yzm, R.id.bt_submit, R.id.bt_cancel})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cdv_yzm:
                if (CourseUtils.isNullOrEmpty(etPhone.getText().toString().trim())){
                    toast("请输入手机号");
                    return;
                }
                EasyHttp.post(this)
                        .api(new GetCodeApi()
                                .setMobile(etPhone.getText().toString()))
                        .request(new HttpCallback<CodeBean>(this) {

                            @Override
                            public void onSucceed(CodeBean data) {
                                toast(data.getMsg());
                                if (data.getCode().equals("200")){
                                    cdvYzm.start();
                                }
                            }
                        });
                break;
            case R.id.bt_submit:
                forget();

                break;
            case R.id.bt_cancel:
                finish();
                break;
        }
    }

    private void forget() {
        EasyHttp.post(this)
                .api(new ForgetNewpwdApi()
                        .setMobile(etPhone.getText().toString()).setYzm(etYzm.getText().toString()))
                .request(new HttpCallback<CodeBean>(this) {

                    @Override
                    public void onSucceed(CodeBean data) {
                        toast(data.getMsg());
                        if (data.getCode().equals("200")){
                            startActivity(new Intent(ForgetPasswordActivity.this,SetNewPasswordActivity.class).putExtra("mobile",etPhone.getText().toString()));
                            finish();
                        }
                    }
                });
    }
}